package core2.ch04database;

import java.sql.*;

public class MssqlDB {
	String sDBDriver;

	String sConnStr;

	String username; 
	String pwd;
	
	Connection conn;

	ResultSet rs;
	

	public MssqlDB() {
		sDBDriver = "net.sourceforge.jtds.jdbc.Driver";
		sConnStr = "jdbc:jtds:sqlserver://localhost:1433/Web";
		username = "sa";
		pwd = "sa";
		conn = null;
		rs = null;
		try {
			Class.forName(sDBDriver);
		} catch (ClassNotFoundException classnotfoundexception) {
			System.err.println("faq(): " + classnotfoundexception.getMessage());
		}
	}

	public void executeInsert(String s) {
		try {
			conn = DriverManager.getConnection(sConnStr, username, pwd);
			Statement statement = conn.createStatement();
			int i = statement.executeUpdate(s);
		} catch (SQLException sqlexception) {
			System.err
					.println("DB.executeUpdate:" + sqlexception.getMessage());
		}
	}

	public ResultSet executeQuery(String s) {
		rs = null;
		try {
			conn = DriverManager.getConnection(sConnStr, username, pwd);
			Statement statement = conn.createStatement();
			rs = statement.executeQuery(s);
		} catch (SQLException sqlexception) {
			System.err.println("DB.executeQuery: " + sqlexception.getMessage());
		}
		return rs;
	}
	public int executeUpdate(String s) {
		int i = 0;
		try {
			conn = DriverManager.getConnection(sConnStr, username, pwd);
			Statement statement = conn.createStatement();
			i = statement.executeUpdate(s);
		} catch (SQLException sqlexception) {
			System.err.println("DB.executeUpate: "
					+ sqlexception.getMessage());
		}
		return i;
	}

	public void executeDelete(String s) {
		try {
			conn = DriverManager.getConnection(sConnStr, username, pwd);
			Statement statement = conn.createStatement();
			statement.executeUpdate(s);
		} catch (SQLException sqlexception) {
			System.err.println("DB.executeDelete: "
					+ sqlexception.getMessage());
		}
	}
	public void Close() {
		try {
			if (rs != null)
				rs.close();
			if (conn != null)
				conn.close();
		} catch (SQLException sqlexception) {
			System.err.println("faq.executeDelete: "
					+ sqlexception.getMessage());
		}
	}
}